package com.seguriapp.common.gwt.client.pagination;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.seguriapp.common.gwt.client.pagination.SearchPaginationPresenter.Display;

public class SearchPagination extends Composite implements Display {

  private static SearchPaginationUiBinder uiBinder = GWT.create(SearchPaginationUiBinder.class);

  interface SearchPaginationUiBinder extends UiBinder<Widget, SearchPagination> {
  }

  private static final String NEXT_TEXT = "Siguiente";
  private static final String PAGE = "Page";
  private static final String PAGE_SELECTED_CSS = "pageSelected";
  public static final int MAX_NEXT_LINKS = 1;

  @UiField
  protected FlowPanel pagination;

  private final ClickHandler paginationHandler;

  private SearchPaginationPresenter presenter;

  public SearchPagination() {
    initWidget(uiBinder.createAndBindUi(this));

    paginationHandler = new ClickHandler() {

      public void onClick(final ClickEvent event) {
        final Object anchor = event.getSource();
        if (anchor instanceof Anchor) {
          final String idPagina = ((Anchor) anchor).getElement().getId();
          final long numeroPagina = Long.parseLong(idPagina.replace(PAGE, ""));
          presenter.changePage(numeroPagina);
        }
      }
    };
  }

  public void setPresenter(final SearchPaginationPresenter presenter) {
    this.presenter = presenter;
  }

  public void setPagination(final long currentPage, final long totalPages, final long maxPages) {
    pagination.clear();
    if (totalPages == 1) {
      return;
    }
    if (totalPages > maxPages) {
      if (currentPage < maxPages / 2 + 1) {
        for (long index = 1; index <= maxPages; index++) {
          addPage(index, false, index == currentPage);
        }
      } else {
        if (currentPage + maxPages / 2 - 1 <= totalPages) {
          for (long index = currentPage - maxPages / 2; index <= currentPage + maxPages / 2 - 1; index++) {
            addPage(index, false, index == currentPage);
          }
        } else {
          for (long index = totalPages - maxPages + 1; index <= totalPages; index++) {
            addPage(index, false, index == currentPage);
          }
        }
      }
    } else {
      for (long index = 1; index <= totalPages; index++) {
        addPage(index, false, index == currentPage);
      }
    }
  }

  private void addPage(final long index, final boolean next, final boolean current) {
    final String pageNumber = String.valueOf(index);
    final Anchor pageLink = new Anchor(next ? NEXT_TEXT : pageNumber);
    pageLink.getElement().setId(PAGE + pageNumber);
    pageLink.addClickHandler(paginationHandler);
    if (current) {
      pageLink.addStyleName(PAGE_SELECTED_CSS);
    }
    pagination.add(pageLink);
  }

}
